package ru.ipo.dces.client.components;

import ru.ipo.dces.client.Controller;
import ru.ipo.dces.clientservercommunication.AvailableContestsRequest;
import ru.ipo.dces.clientservercommunication.AvailableContestsResponse;
import ru.ipo.dces.clientservercommunication.ContestDescription;
import ru.ipo.dces.plugins.admin.beans.ContestsListBean;
import ru.ipo.dces.plugins.admin.components.ContestList;
import ru.ipo.dces.server.ServerFailReason;
import ru.ipo.dces.utils.ResponseHandler;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ChooseContestDialog extends JDialog {

    private ContestList contestsList;
    private JLabel idLabel;
    private ContestDescription selectedContest;

    public ChooseContestDialog(/*JFrame frame*/) {
        super(Controller.getClientDialog());
        initGUI();
    }

    private void initGUI() {
        setModalityType(ModalityType.APPLICATION_MODAL);

        JPanel buttonsPanel = new JPanel();
        add(buttonsPanel, BorderLayout.SOUTH);

        idLabel = new JLabel();
        buttonsPanel.add(idLabel);
        idLabel.setText("");

        JButton okButton = new JButton();
        buttonsPanel.add(okButton);
        okButton.setText("Выбрать");

        JButton cancelButton = new JButton();
        buttonsPanel.add(cancelButton);
        cancelButton.setText("Отменить");

        contestsList = new ContestList();
        JScrollPane contestsListScroll = new JScrollPane(contestsList);
        add(contestsListScroll, BorderLayout.CENTER);

        setSize(400, 300);
        setLocationRelativeTo(null);
        setTitle("Выберите соревнование");

        //add action
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                ContestsListBean val = (ContestsListBean) contestsList.getSelectedValue();
                if (val == null)
                    selectedContest = null;
                else
                    selectedContest = val.getDescription();
                ChooseContestDialog.this.setVisible(false);
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cancelClick();
            }
        });

        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                cancelClick();
            }
        });

        contestsList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                final ContestsListBean value = (ContestsListBean) contestsList.getSelectedValue();
                if (value == null) return;
                idLabel.setText("contest id = " + value.getDescription().contestID);
            }
        });
    }

    private void cancelClick() {
        selectedContest = null;
        this.setVisible(false);
    }

    public void run(final ContestChoosingPanel choosingPanel) {
        choosingPanel.selectSelectContestButtonEnable(false);
        ResponseHandler<AvailableContestsResponse> handler = new ResponseHandler<AvailableContestsResponse>() {
            @Override
            public void success(AvailableContestsResponse response) {
                ContestDescription[] contests = response.contests;

                //wrap in beans
                ContestsListBean[] contestsBeans = new ContestsListBean[contests.length];
                for (int i = 0; i < contests.length; i++)
                    contestsBeans[i] = new ContestsListBean(contests[i]);

                ListModel listModel = new DefaultComboBoxModel(contestsBeans);

                //TODO reuse the same code from ContestChoosing panel, that code also sorts contests and adds admin contest if needed

                contestsList.setModel(listModel);

                selectedContest = null;
                ChooseContestDialog.this.setVisible(true);

                choosingPanel.setContest(selectedContest);
                choosingPanel.selectSelectContestButtonEnable(true);
            }

            @Override
            public void fail(ServerFailReason exception) {
                choosingPanel.setContest(null);
                choosingPanel.selectSelectContestButtonEnable(true);
            }
        };

        Controller.doRequest(new AvailableContestsRequest(), handler, "Получение списка доступных соревнований");
    }

}
